package com.ruoyi.web.controller.business;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.CustomersDto;
import com.ruoyi.common.core.domain.dto.PlatformsDto;
import com.ruoyi.common.core.domain.dto.ProgressDto;
import com.ruoyi.common.core.domain.entity.CustomerProgress;
import com.ruoyi.common.core.domain.entity.Customers;
import com.ruoyi.common.core.domain.entity.Platforms;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.ICustomerService;
import com.ruoyi.system.service.IPlatformService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 客户管理
 * 
 * @author zz
 */
@Controller
@RequestMapping("/system/customer")
public class CustomerController extends BaseController
{
    private String prefix = "business/customer";

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IPlatformService platformService;

    @RequiresPermissions("system:customer:view")
    @GetMapping()
    public String customer()
    {
        return prefix + "/customer";
    }

    @RequiresPermissions("system:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CustomersDto customersDto)
    {
        startPage();
        List<Customers> list = customerService.selectAll(customersDto);
        return getDataTable(list);
    }

    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:customer:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CustomersDto customersDto)
    {
        List<Customers> list = customerService.selectAll(customersDto);
        ExcelUtil<Customers> util = new ExcelUtil<Customers>(Customers.class);
        return util.exportExcel(list, "客户数据");
    }

    /**
     * 新增客户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客户
     */
    @RequiresPermissions("system:customer:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Customers customer)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(customerService.insert(customer));
    }

    /**
     * 修改客户
     */
    @RequiresPermissions("system:customer:edit")
    @GetMapping("/edit/{customerId}")
    public String edit(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        mmap.put("customer", customerService.selectByPrimaryKey(customerId));
        return prefix + "/edit";
    }

    /**
     * 修改保存客户
     */
    @RequiresPermissions("system:customer:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Customers customer)
    {
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(customerService.updateByPrimaryKey(customer));
    }

    /**
     * 逻辑删除客户
     */
    @RequiresPermissions("system:customer:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        customerService.deleteByPrimaryKey(ids);
        return success();
    }

    /**
     * 进入设置平台进度页面
     */
    @RequiresPermissions("system:customer:edit")
    @GetMapping("/enterProgress/{customerId}")
    public String enterProgress(@PathVariable("customerId") Long customerId, ModelMap mmap)
    {
        ProgressDto progressDto = new ProgressDto();
        progressDto.setCustomerId(customerId);
        List<CustomerProgress> results = new ArrayList<>();
        List<Customers> customersList = customerService.getCustomerPlatformsAndProgress(progressDto);
        if(customersList.isEmpty()){
            PlatformsDto platformsDto = new PlatformsDto();
            List<Platforms> platformsList= platformService.selectAll(platformsDto);
            for (Platforms platforms : platformsList) {
                CustomerProgress customerProgress = new CustomerProgress();
                customerProgress.setCustomerId(customerId);
                customerProgress.setPlatformId(platforms.getPlatformId());
                customerProgress.setPlatformName(platforms.getPlatformName());
                customerProgress.setCurrentProgress(0);//初始化0-未处理
                results.add(customerProgress);
            }
        }else {
            for (Customers customers : customersList) {
                CustomerProgress customerProgress = new CustomerProgress();
                customerProgress.setCustomerId(customerId);
                customerProgress.setPlatformId(customers.getPlatformId());
                customerProgress.setPlatformName(customers.getPlatformName());
                customerProgress.setCurrentProgress(customers.getCurrentProgress());//初始化0-未处理
                results.add(customerProgress);
            }
        }
        mmap.put("progressInfos", results);
        mmap.put("customerId", customerId);
        return prefix + "/progress/edit.html";
    }

    /**
     * 新增平台进度
     */
    @Log(title = "新增平台进度", businessType = BusinessType.UPDATE)
    @PostMapping("/addProgress")
    @ResponseBody
    public AjaxResult addProgress(@RequestBody ProgressDto customerProgress)
    {
        if (customerProgress!=null)
        {
            List<Customers> customersList = customerService.getCustomerPlatformsAndProgress(customerProgress);
            if(customersList.size()>0 && customersList !=null){
                //更新
                customerService.updatePlatformProgress(customerProgress);
            }else {
                //新增
                customerService.insertPlatformProgress(customerProgress);
            }
            return success();
        }
        return error();
    }
}